package cn.edu.hhu.teacher;

import cn.edu.hhu.teacher.bean.Course;
import cn.edu.hhu.teacher.bean.Teacher;
import cn.edu.hhu.teacher.service.CourseService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/teacher/addcourse")
public class AddCourseServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cname = request.getParameter("cname");
        if (cname == null || cname.equals("")) {
            request.setAttribute("msg", "课程名不能为空！");
            request.getRequestDispatcher("/WEB-INF/view/teacher/addcourse.jsp").forward(request, response);
            return;
        }
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        Course course = new Course();
        course.setCname(cname);
        course.setTid(teacher.getTid());
        CourseService courseService = new CourseService();
        if (courseService.add(course)) {
            request.setAttribute("msg", "课程添加成功！<br>请返回主页查看或继续添加。");
        } else {
            request.setAttribute("msg", "课程添加失败！");
        }
        request.getRequestDispatcher("/WEB-INF/view/teacher/addcourse.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/view/teacher/addcourse.jsp").forward(request, response);
    }
}
